<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_krs_approve extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.nim';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_krs, a.nim, b.nama, c.nama_dosen, a.ips, a.semester, sum(e.sks) as total_sks, count(e.kd_matakuliah) as total_mk, a.status');
			$this->db->from('tbl_krs a');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');
			$this->db->join('tbl_krs_detail d', 'd.id_krs = a.id_krs','left');
			$this->db->join('ref_matakuliah e', 'e.kd_matakuliah = d.kd_matakuliah','left');
			
			$this->db->where('a.kd_fakultas', $kd_fakultas);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.id_tahunajaran', $tahun);
			
			$this->db->where('a.status !=', 'Approve');
			
			$this->db->group_by('id_krs');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdata_detail($id){
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nama_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_matakuliah, b.nama_matakuliah, b.sks, b.semester');
			$this->db->from('tbl_krs_detail a');
			$this->db->join('ref_matakuliah b', 'b.kd_matakuliah = a.kd_matakuliah');
			$this->db->where('a.id_krs', $id);
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function approve($id, $status){
		$this->db->flush_cache();
		$this->db->set('status', $status);
		$this->db->where('id_krs', $id);
		
		return $this->db->update('tbl_krs');
	}
	
}

?>